package waggle.client;

import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;

import waggle.client.View.ViewStateChangeListener;

public abstract class ComponentView extends View implements
		ViewStateChangeListener {

	// 基本名稱

	protected static final String MAP = "Map";
	protected static final String LIST = "List";
	protected static final String DETAIL = "Detail";
	protected static final String UPLOAD = "Upload";
	protected static final String ADVANCE = "Advance";
	  /**
	   * The common style name for all views.
	   */
	  protected static final String STYLE_NAME = "View-Content content";

	// history
	  
	  /**
	   * Override this method is this view need to handle history changed.
	   */
	  protected boolean isMyView(String value) {
	    return false;
	  }
	  
	/**
	 * Override this method is this view need to handle history changed.
	 */
	protected void handleOnValueChange(String historyToken) {

	}

	/**
	 * Calls parent addHistoryItem() if historyButtonClicked is true which mean
	 * refresh, back, or forward browser button is clicked.
	 * 
	 * @see org.rebioma.client.View#addHistoryItem(boolean)
	 */

	public void addHistoryItem(boolean issueEvent) {
		if (!parent.historyButtonClicked && !historyButtonClicked) {
			parent.addHistoryItem(issueEvent);
		}
		parent.historyButtonClicked = false;
		historyButtonClicked = false;
	}

	  /**
	   * Override this method is this view have to construct its own history token
	   */

	  public String historyToken() {
	    return "";
	  }
	
	  /**
	   * Override this method is this view need to handle onStateChange
	   * {@link ViewStateChangeListener}.
	   */

	  public void onStateChanged(ViewState state) {

	  }

	protected void addStateChangeListener(ViewStateChangeListener listener) {
		parent.addStateChangeListener(listener);
		// if (parent instanceof ComponentView) {
		// ((ComponentView) parent).addStateChangeListener(listener);
		// } else {
		// super.addStateChangeListener(listener);
		// }
	}

	
	// resize

	  /**
	   * Gets called when browser size changed.
	   */
	  public void onResize(ResizeEvent event) {
	    if (isMyView(History.getToken())) {
	      resize(event.getWidth(), event.getHeight());
	    }
	  }
	
	/**
	 * Override this method to handle on browser window resize instead of
	 * {@link #onResize(ResizeEvent)}
	 * 
	 * @param width
	 * @param height
	 */
	protected void resize(int width, int height) {

	}

	// 基本項目
	View parent;
	
	  /**
	   * Called before the view is displayed.
	   */
	  public void onShow() {
	    DeferredCommand.addCommand(new Command() {
	      public void execute() {
	        if (isMyView(History.getToken())) {
	          resize(Window.getClientWidth(), Window.getClientHeight());
	        }
	      }

	    });
	    if (isMyView(History.getToken())) {
	      resize(Window.getClientWidth(), Window.getClientHeight());
	    }
	  }
	

	  protected final boolean isDefaultView;
	  boolean isDefaultView() {
		    return isDefaultView;
		  }

	  public ComponentView(View parent, boolean isDefaultView) {
		    this.parent = parent;
		    this.isDefaultView = isDefaultView;
		  }
	  
}
